import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

pd.set_option('display.max_columns', None,
              'display.expand_frame_repr', False)
spr = 1  # sub plot rows
spc = 1  # sub plot columns
spn = 0  # sub plot number
plt.figure(figsize=[6, 6])


def sep(label=''):
    print('-' * 32, label, '-' * 32, sep='')


# 3．针对乳腺癌数据，完成以下分类处理操作（每题1.5分）
# （1）数据处理
# ①获取breast-cancer-wisconsin.data数据
sep('①获取breast-cancer-wisconsin.data数据')
df = pd.read_csv(r'../../../../../large_data/ML2/breast-cancer-wisconsin.data.csv',
                 index_col=0,
                 header=None)
m = len(df)
print(df[:5])

# ②处理异常值
sep('②处理异常值')
print(df.shape)
m, n = df.shape
for i in range(n):
    df.iloc[:, i] = pd.to_numeric(df.iloc[:, i], errors='coerce')  # ATTENTION pd.to_numeric
df.dropna(axis=0, inplace=True)
print(df.shape)
# ③将标签调整为0,1形式
sep('③将标签调整为0,1形式')
print(df.iloc[:, -1].value_counts().index)
from sklearn.preprocessing import LabelEncoder
enc = LabelEncoder()
df.iloc[:, -1]= enc.fit_transform(df.iloc[:, -1])
print(df.iloc[:, -1].value_counts().index)

# ④检验样本是否平衡，不平衡的话进行护理
print(df.iloc[:, -1].value_counts())
print('不平衡')
idx_y0 = df.iloc[:, -1] == 0
idx_y1 = df.iloc[:, -1] == 1
df = pd.concat([df.loc[idx_y0], df.loc[idx_y1], df.loc[idx_y1]], axis=0)  # ATTENTION pd.concat
print(df.iloc[:, -1].value_counts())

# ⑤对特征进行特征缩放
x = df.iloc[:, :-1]
from sklearn.preprocessing import StandardScaler
std = StandardScaler()
x = std.fit_transform(x)

# ⑥切分训练集和测试集
y = df.iloc[:, -1]
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, train_size=0.7, random_state=666)

# （2）模型处理
# ①创建KNN算法，k设定为3
from sklearn.neighbors import KNeighborsClassifier
clf = KNeighborsClassifier(n_neighbors=3)
clf.fit(x_train, y_train)
print(f'Training score = {clf.score(x_train, y_train)}')
print(f'Testing score = {clf.score(x_test, y_test)}')

# ②对数据进行预测
h_test = clf.predict(x_test)
proba_test = clf.predict_proba(x_test)[:, 1]

# ③打印模型auc，准确率，混淆矩阵数据
sep('③打印模型auc，准确率，混淆矩阵数据')
from sklearn.metrics import roc_curve, roc_auc_score, accuracy_score, confusion_matrix
print(f'模型auc = {roc_auc_score(y_test, proba_test)}')
print(f'模型准确率 = {accuracy_score(y_test, h_test)}')
print('混淆矩阵')
print(confusion_matrix(y_test, h_test))

# ④绘制auc曲线
spn += 1
plt.subplot(spr, spc, spn)
plt.title('ROC')
fpr, tpr, thresold = roc_curve(y_test, proba_test)
plt.plot(fpr, tpr)

# Finally show all plotting
plt.show()
